package it11.Capood.GUI;

import it11.Capood.R;
import it11.Capood.DataLogic.Message;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MessageAdapter extends ArrayAdapter<Message> {
	
	//Quelle: http://www.ezzylearning.com/tutorial.aspx?tid=1763429 (27.06.2013)
	
	Context context;
	int layoutResourceId;
	List<Message> data;
	
	public MessageAdapter(Context context, int textViewResourceId,
			List<Message> objects) {
		super(context, textViewResourceId, objects);
		
		this.context = context;
		layoutResourceId = textViewResourceId;
		data = objects;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent){
		
		View row = convertView;
        MessageHolder holder = null;
       
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
           
            holder = new MessageHolder();
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgView_history_item);
            holder.txtTitle = (TextView)row.findViewById(R.id.txtView_history_item);
           
            row.setTag(holder);
        }
        else
        {
            holder = (MessageHolder)row.getTag();
        }
       
        Message message = data.get(position);
        holder.txtTitle.setText(message.toString());
        
        int id = R.drawable.ic_launcher;
        if (message.getType() == Message.Type.TEXT) {
        	id = R.drawable.default_document;
        } else if (message.getType() == Message.Type.FILE_INFO){
        	id = R.drawable.folder_documents;
        }
        
        holder.imgIcon.setImageResource(id);
       
        return row;
    }
   
    static class MessageHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
    }
}